package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.Shiwu;
import com.ruoyi.system.domain.Zhuanjia;
import com.ruoyi.system.domain.ZhuanjiaDept;
import com.ruoyi.system.service.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.RenwuMapper;
import com.ruoyi.system.domain.Renwu;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-19
 */
@Service
public class RenwuServiceImpl implements IRenwuService 
{
    @Autowired
    private RenwuMapper renwuMapper;

    @Autowired
    private IShiwuService shiwuService;

    @Autowired
    private IZhuanjiaService zhuanjiaService;

    @Autowired
    private IZhuanjiaDeptService zhuanjiaDeptService;
    /**
     * 查询【请填写功能名称】
     * 
     * @param renwuid 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public List<Map<String,Object>> existShenhezhongShiwuByZhuanjiaId(String ppzhuanjiaID)
    {
        return renwuMapper.existShenhezhongShiwuByZhuanjiaId(ppzhuanjiaID);
    }

    @Override
    public int getMaxOrdernumByShiwuId(String pp事务ID)
    {
        Object mmCount = renwuMapper.getMaxOrdernumByShiwuId(pp事务ID);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    @Override
    public Renwu selectRenwuByRenwuid(String renwuid)
    {
        return renwuMapper.selectRenwuByRenwuid(renwuid);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param renwu 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Renwu> selectRenwuList(Renwu renwu)
    {
        return renwuMapper.selectRenwuList(renwu);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param renwu 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertRenwu(Renwu renwu)
    {
        return renwuMapper.insertRenwu(renwu);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param renwu 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateRenwu(Renwu renwu)
    {
        return renwuMapper.updateRenwu(renwu);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param renwuids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteRenwuByRenwuids(String renwuids)
    {
        return renwuMapper.deleteRenwuByRenwuids(Convert.toStrArray(renwuids));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param renwuid 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteRenwuByRenwuid(String renwuid)
    {
        return renwuMapper.deleteRenwuByRenwuid(renwuid);
    }


    @Override
    @Transactional
    public boolean submit_custom(Renwu ppRenwu, Shiwu ppShiwu){
        boolean ok = true;

        int shiwumodify = shiwuService.updateShiwu(ppShiwu);
        if (shiwumodify!=1){
            ok = false;
            throw new RuntimeException("修改事务失败");
        }
        int renwuadd = renwuMapper.insertRenwu(ppRenwu);
        if (renwuadd!=1){
            ok = false;
            throw new RuntimeException("添加任务失败");
        }
        return ok;
    }

    @Override
    public List<Map<String,Object>> findBdShenheListByDept(String shenpideptid,
                                                           int shifoutongguo,
                                                           int shifouwancheng,
                                                           String biandongleixing,
                                                           String deptname,
                                                           String xingming,
                                                           String xiafashijiankaishi,
                                                           String xiafashijianjieshu,
                                                           String wanchengshijiankaishi,
                                                           String wanchengshijianjieshu,
                                                           int ppPageIndex,
                                                           int ppPageSize){

        int mmStartIndex = (ppPageIndex - 1) * ppPageSize;
        if(!deptname.equals("")){
            deptname = "%"+deptname+"%";
        }
        if(!xingming.equals("")){
            xingming = "%"+xingming+"%";
        }
        return renwuMapper.findBdShenheListByDept(shenpideptid,shifoutongguo,shifouwancheng,biandongleixing,deptname,xingming,
                xiafashijiankaishi,xiafashijianjieshu,wanchengshijiankaishi,wanchengshijianjieshu,mmStartIndex,ppPageSize);
    }

    @Override
    public int countBdShenheListByDept(String shenpideptid,
                                       int shifoutongguo,
                                       int shifouwancheng,
                                       String biandongleixing,
                                       String deptname,
                                       String xingming,
                                       String xiafashijiankaishi,
                                       String xiafashijianjieshu,
                                       String wanchengshijiankaishi,
                                       String wanchengshijianjieshu){
        if(!deptname.equals("")){
            deptname = "%"+deptname+"%";
        }
        if(!xingming.equals("")){
            xingming = "%"+xingming+"%";
        }

        return renwuMapper.countBdShenheListByDept(shenpideptid,shifoutongguo,shifouwancheng,biandongleixing,deptname,xingming,
                xiafashijiankaishi,xiafashijianjieshu,wanchengshijiankaishi,wanchengshijianjieshu);
    }

    @Override
    public List<Map<String,Object>> findspjl(String shiwuid){
        return renwuMapper.findspjl(shiwuid);
    }

    @Override
    @Transactional
    public boolean shenpi(Renwu oldrenwu, Renwu newRenwu, Shiwu ppShiwu, Zhuanjia ppZhuanji){
        int ok = 0;
        if(oldrenwu!=null){
            ok = renwuMapper.updateRenwu(oldrenwu);
        }
        if (ok!=1){
            throw new RuntimeException("修改任务失败");
        }

        if(newRenwu!=null){
            ok = renwuMapper.insertRenwu(newRenwu);
        }
        if (ok!=1){
            throw new RuntimeException("添加任务失败");
        }

        if(ppShiwu!=null){
            ok = shiwuService.updateShiwu(ppShiwu);
        }
        if (ok!=1){
            throw new RuntimeException("修改事务失败");
        }
        if(ppZhuanji!=null){
            ok =  zhuanjiaService.updateZhuanjia(ppZhuanji);
        }
        if(ok!=1){
            throw new RuntimeException("更新专家信息失败");
        }

        return true;
    }

    @Override
    @Transactional
    public boolean shenpiZl(Renwu oldrenwu, Renwu newRenwu, Shiwu ppShiwu, ZhuanjiaDept oldZhuanjidept,ZhuanjiaDept newZhuanjidept,Zhuanjia oldzhuanjia,Zhuanjia newZhuanjia){

        int ok = 0;
        if(oldrenwu!=null){
            ok = renwuMapper.updateRenwu(oldrenwu);
        }
        if (ok!=1){
            throw new RuntimeException("修改任务失败");
        }

        if(newRenwu!=null){
            ok = renwuMapper.insertRenwu(newRenwu);
        }
        if (ok!=1){
            throw new RuntimeException("添加任务失败");
        }

        if(ppShiwu!=null){
            ok = shiwuService.updateShiwu(ppShiwu);
        }
        if (ok!=1){
            throw new RuntimeException("修改事务失败");
        }

        if(oldZhuanjidept!=null){
            ok =  zhuanjiaDeptService.updateZhuanjiaDept(oldZhuanjidept);
        }
        if(ok!=1){
            throw new RuntimeException("更新专家单位所属信息失败");
        }

        if(newZhuanjidept!=null){
            ok =  zhuanjiaDeptService.insertZhuanjiaDept(newZhuanjidept);
        }
        if(ok!=1){
            throw new RuntimeException("添加专家单位所属信息失败");
        }
        if(oldzhuanjia!=null){
            ok =  zhuanjiaService.updateZhuanjia(oldzhuanjia);
        }
        if(ok!=1){
            throw new RuntimeException("更新专家信息失败");
        }

        if(newZhuanjia!=null){
            ok =  zhuanjiaService.insertZhuanjia(newZhuanjia);
        }
        if(ok!=1){
            throw new RuntimeException("添加专家信息失败");
        }
        return true;
    }
}
